package fy.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import fy.util.DBUtil;

//类名
public class PictureBean {
	//属性（成员变量）
		private int id;
		private String picture;
		private String title;
		private int homepage;
		private String category;
		private String link;
		private String categoryname;

		PreparedStatement statement=null;
		ResultSet rs = null;
		static Connection connection = null;
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getPicture() {
			return picture;
		}
		public void setPicture(String picture) {
			this.picture = picture;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getCategory() {
			return category;
		}
		public void setCategory(String i) {
			this.category = i;
		}
		public String getLink() {
			return link;
		}
		public void setLink(String link) {
			this.link = link;
		}
		public String getCategoryname() {
			return categoryname;
		}
		public void setCategoryname(String categoryname) {
			this.categoryname = categoryname;
		}
		//查询全部内容
		public List<PictureBean> findAll() {
			List<PictureBean> list = new ArrayList<PictureBean>();
			DBUtil dbUtil = new DBUtil();
			String sql = "select * from picture";
			connection = dbUtil.geConnection();
			try {
				statement = connection.prepareStatement(sql);
				rs = statement.executeQuery();
				while(rs.next()) {
					PictureBean pictureBean = new PictureBean();
					pictureBean.setId(rs.getInt("id"));
					pictureBean.setTitle(rs.getString("title"));
					pictureBean.setPicture(rs.getString("picture"));
					pictureBean.setHomepage(rs.getInt("homepage"));
					pictureBean.setLink(rs.getString("link"));
					pictureBean.setCategory(rs.getString("category"));
					pictureBean.setCategoryname(rs.getString("categoryname"));
					list.add(pictureBean);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				dbUtil.close();
			}
			return list;
			
		}
		
	
		
		
		//增加页面内容
		public boolean doAdd(String picture,String title,String category,int homepage,String link,String categoryname)
		{
			boolean add = false;
			DBUtil dbUtil = new DBUtil();
			connection = dbUtil.geConnection();
			//String sql = "select password from admin where username = " + username + "'";
			String sql = "insert into picture(picture,title,category,homepage,link,categoryname) values(?,?,?,?,?,?)";
			
		  try {		
			  PreparedStatement statement = connection.prepareStatement(sql);
				statement.setString(1, picture);
				statement.setString(2, title);
				statement.setString(3, category);
				statement.setInt(4, homepage);
				statement.setString(5, link);
				statement.setString(6, categoryname);
				System.out.println("结果前" + rs);
				System.out.println(add);
				int i = statement.executeUpdate();
				if (i>0) {
					add = true;
				}
					
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				dbUtil.close();
			}
			return add;
			
		}
		
		//修改内容
		public  boolean Updata(String picture,String title,String category,int homepage,String link,String categoryname,int id) {
			boolean updata = false;
			DBUtil dbUtil = new DBUtil();
			connection = dbUtil.geConnection(); 
			String sql ="update picture set picture=?,title=?,category=?,homepage=?, link=?,categoryname=? where id=? ";

			try {
				PreparedStatement statement = connection.prepareStatement(sql);
				statement.setString(1, picture);
				statement.setString(2, title);
				statement.setString(3, category);
				statement.setInt(4, homepage);
				statement.setString(5, link);
				statement.setString(6, categoryname);
				statement.setInt(7, id);
				System.out.println(updata);
				int i = statement.executeUpdate();
				if (i>0) {
					updata = true;
				}
					
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				dbUtil.close();
			}
			return updata;
			
		}
		
		
		//通过ID查找
		public PictureBean findNewsById(int id) {
			
			DBUtil dbUtil = new DBUtil();
			connection = dbUtil.geConnection();
			PictureBean pictureBean = new PictureBean();
			try {
				String sql = "select * from picture where id = ?";
				statement = connection.prepareStatement(sql);
				statement.setInt(1, id);
				rs = statement.executeQuery();
				
				if(rs.next()) {
					pictureBean.setId(rs.getInt("id"));
                    pictureBean.setCategory(rs.getString("category"));
					pictureBean.setTitle(rs.getString("title"));
					pictureBean.setPicture(rs.getString("picture"));
					pictureBean.setHomepage(rs.getInt("homepage"));
					pictureBean.setLink(rs.getString("link"));
                    pictureBean.setCategoryname(rs.getString("categoryname"));
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				dbUtil.close();
			}

			return pictureBean;
			
		}
		//通过类别查找

		public int getHomepage() {
			return homepage;
		}
		public void setHomepage(int homepage) {
			this.homepage = homepage;
		}
		public List<PictureBean> findsfgkByLb(String lbString) {
			List<PictureBean> list = new ArrayList<PictureBean>();
			DBUtil dbUtil = new DBUtil();
			connection = dbUtil.geConnection();
			String sql = "select * from picture where category =? and homepage=1";
			try {
				statement = connection.prepareStatement(sql);
				statement.setString(1, lbString);
				rs = statement.executeQuery();
				while(rs.next()) {
					PictureBean pictureBean = new PictureBean();
					pictureBean.setId(rs.getInt("id"));
					pictureBean.setTitle(rs.getString("title"));
					pictureBean.setPicture(rs.getString("picture"));
					pictureBean.setHomepage(rs.getInt("homepage"));
					pictureBean.setLink(rs.getString("link"));
					pictureBean.setCategory(rs.getString("category"));
					pictureBean.setCategoryname(rs.getString("categoryname"));
					list.add(pictureBean);
				}


			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				dbUtil.close();
			}
			return list;
			
		}


		
		
		
		//删除内容
		public boolean doDel(int id) {
			boolean del = false;
			DBUtil dbUtil = new DBUtil();
			connection = dbUtil.geConnection();
			String sql = "delete from  picture where id =?";
			//String sql = "insert into sfgk(v1,v2) values(?,getdate());";
		    //String sql = "insert into sfgk(category,title,source,content,author,id) values(?,?,?,?,?,?)";
			
		  try {		
			  PreparedStatement statement = connection.prepareStatement(sql);
		
				statement.setInt(1, id);
				int i = statement.executeUpdate();
				if (i>0) {
					del = true;
				}
					
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				dbUtil.close();
			}
			return del;
			
		}
		
		
		@Override
		public String toString() {
			return "SfgkBean [id=" + id + ", picture=" + picture + ", homepage=" + homepage + ",link="+link+ "category="+ category +"categoryname="+ categoryname + "]";
		}

		public static void main(String[] args) {
			
		}


}
